package com.huluobo.a2110mvvmjetpackdemo2.lifecycle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.huluobo.a2110mvvmjetpackdemo2.viewmodel.ADViewModel;
import com.huluobo.a2110mvvmjetpackdemo2.viewbinding.MainActivity;
import com.huluobo.a2110mvvmjetpackdemo2.R;

/**
 * LifeCycle总结:
 * 1.如果需要使用LifeCycle,首先需要实现一个接口,LifeCycleObserver,让实现他的类可以去感知宿主的生命周期(观察者)
 * 2.需要一个宿主对象,实现过LifeCycleOwner,一般来说AppCompatActivity 已经实现了宿主接口,不需要再次实现(被观察者)
 * 3.通过getLifecycle.addObserver来产生订阅关系(订阅关系)
 * 4.可以有效解决宿主对象中控制其他控件的生命周期,让观察者可以根据宿主对象的生命周期自行做出判断来调用哪些方法
 * 5.MVVM模式的基础是观察者模式,也是基于LifeCycle
 */
public class ADActivity extends AppCompatActivity {

    private TextView time;
    private Button skip;
    private ADManager adManager;
    private ADViewModel adViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adactivity);

        time = findViewById(R.id.time);
        skip = findViewById(R.id.skip);
//        if (savedInstanceState != null) {
//            //从savedInstanceState 取出暂存的广告剩余时间
//            millisInFuture = savedInstanceState.getLong("time");
//        }

        adViewModel = new ViewModelProvider(this).get(ADViewModel.class);
        adViewModel.getTimingResult().observe(this, new Observer<Long>() {
            @Override
            public void onChanged(Long aLong) {
                time.setText("广告剩余时间:" + aLong + "秒");
                if (aLong == 1) {
                    Intent intent = new Intent(ADActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });

        adManager = new ADManager(adViewModel);
        //订阅关系
        getLifecycle().addObserver(adManager);
//        adManager.setAdManagerListener(new ADManager.ADManagerListener() {
//            @Override
//            public void timing(int second) {
//                time.setText("广告剩余时间:" + second + "秒");
//                adViewModel.millisInFuture = second * 1000L;
//            }
//
//            @Override
//            public void finish() {
//                Intent intent = new Intent(ADActivity.this, MainActivity.class);
//                startActivity(intent);
//                ADActivity.this.finish();
//            }
//        });

        skip.setOnClickListener(v -> {
            Intent intent = new Intent(ADActivity.this, MainActivity.class);
            startActivity(intent);
            ADActivity.this.finish();
        });
    }

//    //当activity生命周期发生改变的时候,可以把一些内容存到bundle中,进行暂存
//    @Override
//    protected void onSaveInstanceState(@NonNull Bundle outState) {
//        super.onSaveInstanceState(outState);
//        outState.putLong("time", millisInFuture);
//    }
}